package treeSet;

// TreeSet集合存储Student类型，需要实现Comparable接口
public class Student implements Comparable<Student> {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}' + "\n";
    }

    @Override
    public int compareTo(Student o) {
        // 按照对象的年龄进行排序
        // 主要条件
        int result = this.age - o.age;
        // 次要条件
        return result == 0 ? this.name.compareTo(o.getName()) : result;
//        return result != 0 ? result : this.name.compareTo(o.name);
    }
}
